package com.ruoyi.web.controller.wechatApplet;

import com.ruoyi.background.domain.AppImg;
import com.ruoyi.background.service.IAppImgService;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 小程序公共入口
 */
@RequestMapping("/wechat/common")
@RestController
public class WechatCommonController extends BaseController {

    @Autowired
    private IAppImgService appImgService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 通过请求索引请求图片
     *
     * @param siteIndex 图片的索引
     * @return 结果
     */
    @GetMapping("/getImg/{siteIndex}")
    public AjaxResult getImgBySiteIndex(@PathVariable String siteIndex) {
        String key = REDIS_KEY.APP_IMG_KEY + siteIndex;
        AppImg img = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(img)) {
            img = appImgService.selectAppImgBySiteIndex(siteIndex);
            redisCache.setCacheObject(key, img);
            redisCache.expire(key, 24 * 60, TimeUnit.MINUTES);
        }
        return AjaxResult.success(img);
    }


}
